package cn.sxw.android.base.utils;

import android.os.Parcelable;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.tencent.mmkv.MMKV;

import java.util.Set;

/**
 * Created by ZengCS on 2019/3/18.
 * E-mail:zcs@sxw.cn
 * Add:成都市天府软件园E3-3F
 * MMKVUtils
 */
public class MMKVUtils {
    public static Object get(@NonNull String key, @NonNull Object dftValue) {
        MMKV mmkv = MMKV.defaultMMKV();
        String type = dftValue.getClass().getSimpleName();
        if ("String".equals(type)) {
            return mmkv.decodeString(key, (String) dftValue);
        } else if ("Integer".equals(type)) {
            return mmkv.decodeInt(key, (Integer) dftValue);
        } else if ("Float".equals(type)) {
            return mmkv.decodeFloat(key, (Float) dftValue);
        } else if ("Long".equals(type)) {
            return mmkv.decodeLong(key, (Long) dftValue);
        } else if ("Double".equals(type)) {
            return mmkv.decodeDouble(key, (Double) dftValue);
        } else if ("Boolean".equals(type)) {
            return mmkv.decodeBool(key, (Boolean) dftValue);
        }
        return null;
    }

    public static boolean put(@NonNull String key, @NonNull Object value) {
        MMKV mmkv = MMKV.defaultMMKV();

        String type = value.getClass().getSimpleName();
        if ("String".equals(type)) {
            return mmkv.encode(key, (String) value);
        } else if ("Integer".equals(type)) {
            return mmkv.encode(key, (Integer) value);
        } else if ("Float".equals(type)) {
            return mmkv.encode(key, (Float) value);
        } else if ("Long".equals(type)) {
            return mmkv.encode(key, (Long) value);
        } else if ("Double".equals(type)) {
            return mmkv.encode(key, (Double) value);
        } else if ("Boolean".equals(type)) {
            return mmkv.encode(key, (Boolean) value);
        }
        return false;
    }

    public static byte[] getBytes(@NonNull String key) {
        MMKV mmkv = MMKV.defaultMMKV();
        return mmkv.decodeBytes(key);
    }

    public static boolean putBytes(@NonNull String key, byte[] value) {
        MMKV mmkv = MMKV.defaultMMKV();
        return mmkv.encode(key, value);
    }

    public static <T extends Parcelable> T getParcelable(@NonNull String key, Class<T> tClass) {
        MMKV mmkv = MMKV.defaultMMKV();
        return mmkv.decodeParcelable(key, tClass);
    }

    public static boolean putParcelable(@NonNull String key, Parcelable value) {
        MMKV mmkv = MMKV.defaultMMKV();
        return mmkv.encode(key, value);
    }

    public static Set<String> getStringSet(@NonNull String key, @Nullable Set<String> dftVal) {
        MMKV mmkv = MMKV.defaultMMKV();
        if (dftVal != null)
            return mmkv.decodeStringSet(key, dftVal);
        return mmkv.decodeStringSet(key);
    }

    public static boolean putStringSet(@NonNull String key, Set<String> value) {
        MMKV mmkv = MMKV.defaultMMKV();
        return mmkv.encode(key, value);
    }

    //移除指定key的值
    public static void removeKey(@NonNull String key){
        MMKV mmkv = MMKV.defaultMMKV();
        mmkv.removeValueForKey(key);
    }

    // ****************************** 没有默认值，自动填充默认值 ******************************
    public static String getString(@NonNull String key) {
        return getString(key, "");
    }

    public static Boolean getBoolean(@NonNull String key) {
        return (Boolean) get(key, Boolean.FALSE);
    }

    public static Integer getInteger(@NonNull String key) {
        return (Integer) get(key, 0);
    }

    public static Float getFloat(@NonNull String key) {
        return (Float) get(key, 0F);
    }

    public static Double getDouble(@NonNull String key) {
        return (Double) get(key, 0D);
    }

    public static Long getLong(@NonNull String key) {
        return (Long) get(key, 0L);
    }

    // ****************************** 有默认值，默认值不能为NULL ******************************
    public static String getString(@NonNull String key, @NonNull String dftVal) {
        return (String) get(key, dftVal);
    }

    public static Boolean getBoolean(@NonNull String key, @NonNull Boolean dftVal) {
        return (Boolean) get(key, dftVal);
    }

    public static Integer getInteger(@NonNull String key, @NonNull Integer dftVal) {
        return (Integer) get(key, dftVal);
    }

    public static Float getFloat(@NonNull String key, @NonNull Float dftVal) {
        return (Float) get(key, dftVal);
    }

    public static Double getDouble(@NonNull String key, @NonNull Double dftVal) {
        return (Double) get(key, dftVal);
    }

    public static Long getLong(@NonNull String key, @NonNull Long dftVal) {
        return (Long) get(key, dftVal);
    }
}
